package com.hly.musicbackground.model.music.rest;

import com.hly.musicbackground.Final.SongLinkFinal;
import com.hly.musicbackground.Utils.RequestContextUtils;
import com.hly.musicbackground.domain.Song;
import com.hly.musicbackground.model.music.VO.MusicListVO;
import com.hly.musicbackground.model.music.VO.SongDetailVO;
import com.hly.musicbackground.model.music.VO.SongQueryDataVO;
import com.hly.musicbackground.model.music.params.MusicRidParams;
import com.hly.musicbackground.model.music.params.SongQueryParams;
import com.hly.musicbackground.model.music.service.SongLinkService;
import com.hly.musicbackground.model.music.service.SongService;
import com.hly.musicbackground.result.Result;
import com.hly.musicbackground.result.Token;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 歌曲列表
 * */
@RestController
@RequestMapping("/api/song")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SongController {
    private final SongService songService;
    private final SongLinkService songLinkService;


    @GetMapping("/error")
    @ApiOperation(value = "获取播放错误的歌曲")
    public Result<List<Song>> musicMp3(){
        return Result.ok(songService.getErrorList());
    }

    @GetMapping("/sort")
    @ApiOperation(value = "获取排序音乐列表")
    public Result<List<Song>> getIndex(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "6") int limit,
                                       @RequestParam(defaultValue = "mp3") String type,
                                       @RequestParam(defaultValue = "desc") String sort){
        return Result.ok(songService.getSortList(type, sort, page, limit));
    }

    @GetMapping("/list")
    @ApiOperation(value = "筛选音乐列表")
    public Result<MusicListVO> getQuerySongList(SongQueryParams params){
        return Result.ok(songService.getQueryList(params));
    }

    @GetMapping("/queryData")
    @ApiOperation(value = "获取音乐筛选条件")
    public Result<List<SongQueryDataVO>> songQueryData(SongQueryParams params){
        return Result.ok(songService.songQueryData());
    }

    @GetMapping("/detail/{rid}")
    @ApiOperation(value = "获取音乐详情")
    public Result<SongDetailVO> detail(@PathVariable("rid") int rid){
        SongDetailVO detail = songService.getDetail(rid);
        Token token = RequestContextUtils.getToken();
        if (token != null){
            detail.setIsLike(songLinkService.isBoolean(SongLinkFinal.LIKE, token.getId(), rid));
        }
        return Result.ok(detail);
    }
}
